<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/19
 * Time: 10:53
 */

namespace app\api\controller;


use app\api\model\UserShop;
use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;

class Goods extends Controller
{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> gid = $request->param('gid');
        $this -> openId = $request -> param('openid');
    }

    //列表
    public function index(){
        $res = db('goods_type')->field('type_id,type_name')->select();
        foreach ($res as $key => $val){
//            if($val['type_id']){
//                dump($key);
                $val['goods'] = db('goods')->where('type_id','=',$val['type_id'])->field('goods_id,g_name,g_img,g_price,g_introduce')->select();
                foreach ($val['goods'] as $k =>$v){
                    $hasspec = db('goods_spec')->where('goods_id','=',$v['goods_id'])->select();
                    if($hasspec){
                        foreach ($hasspec as $e => $s){
                            foreach ($s as $ke => $va){
                                if(!empty($va) ){
                                    if($ke == 'spec' || $ke == 'dregs'|| $ke == 'sugar' || $ke == 'flavor' || $ke == 'state' || $ke == 'tem'){
                                        $arr = explode(',',$va);
                                        foreach ($arr as $i => $value){
                                            $s_data = db('set_'.$ke)->where($ke.'_id','=',$value)->field($ke.'_id,'.$ke.'_name')->find();
//                        dump($s_data);
                                            $s_data['add_price'] = 0;
                                            $arr[$i] = $s_data;

                                        }
                                        $va = $arr;
                                    }else if($ke == 'feed'){
                                        $arr = explode(',',$va);
                                        foreach ($arr as $i => $value){
//                        dump($v);
                                            $IndexId = substr($value,0,strpos($value,'¥'));
//                        dump($IndexId);
                                            $s_data = db('set_feed')->where('feed_id','=',$IndexId)->field('feed_id,feed_name')->find();
//                        dump($s_data);
                                            $s_data['add_price'] = trim(strrchr($value,'¥'),'¥');
                                            $arr[$i] = $s_data;
                                        }
                                        $va = $arr;
                                    }
                                }
                                $s[$ke] = $va;
                            }
//                            dump($s);
                            $hasspec[$e] = $s;
                        }
//                        dump($hasspec);
                    }
//                    if($hasspec){
//                        $v['has_spec'] = 1;
//                    }else{
//                        $v['has_spec'] = 0;
//                    }
                    $val['goods'][$k]['hasspec'] = $hasspec;
//                    dump($key);
//                    dump($hasspec);
                }
//
                $res[$key] = $val;

//            }
        }
//        die;
        if($res){
            return json(['code' => 0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
        }
        return json(['code'=>5,'msg'=>ReturnCode::$return_code[5]]);
    }
    //规格
    public function goodsinfo(){
        if(empty($this -> gid)){
            return json(['code'=>2,'msg'=>ReturnCode::$return_code[2]]);
        }
        $res = db('goods_spec')->where('goods_id','=',$this->gid)->find();
        if($res){
            foreach ($res as $key => $val){
//            dump($val);
                if(!empty($val)){
//                dump($key);
                    if($key == 'spec'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
                            $s_data = db('set_spec')->where('spec_id','=',$v)->field('spec_id,spec_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = 0;
                            $arr[$k] = $s_data;

                        }
                        $val = $arr;
                    }
                    if($key == 'feed'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
//                        dump($v);
                            $IndexId = substr($v,0,strpos($v,'¥'));
//                        dump($IndexId);
                            $s_data = db('set_feed')->where('feed_id','=',$IndexId)->field('feed_id,feed_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = trim(strrchr($v,'¥'),'¥');
                            $arr[$k] = $s_data;
                        }
                        $val = $arr;
                    }
                    if($key == 'dregs'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
                            $s_data = db('set_dregs')->where('dregs_id','=',$v)->field('dregs_id,dregs_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = 0;
                            $arr[$k] = $s_data;
                        }
                        $val = $arr;
                    }
                    if($key == 'sugar'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
                            $s_data = db('set_sugar')->where('sugar_id','=',$v)->field('sugar_id,sugar_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = 0;
                            $arr[$k] = $s_data;
                        }
                        $val = $arr;
                    }
                    if($key == 'flavor'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
                            $s_data = db('set_flavor')->where('flavor_id','=',$v)->field('flavor_id,flavor_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = 0;
                            $arr[$k] = $s_data;
                        }
                        $val = $arr;
                    }
                    if($key == 'state'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
                            $s_data = db('set_state')->where('state_id','=',$v)->field('state_id,state_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = 0;
                            $arr[$k] = $s_data;
                        }
                        $val = $arr;
                    }
                    if($key == 'tem'){
                        $arr = explode(',',$val);
                        foreach ($arr as $k => $v){
                            $s_data = db('set_tem')->where('tem_id','=',$v)->field('tem_id,tem_name')->find();
//                        dump($s_data);
                            $s_data['add_price'] = 0;
                            $arr[$k] = $s_data;
                        }
                        $val = $arr;
                    }
                    $res[$key] = $val;
                }
            }
            return json(['code'=>0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
        }
        return json(['code'=>10, 'msg' => ReturnCode::$return_code[10]]);
    }
    //购物袋列表 [] 数组
//    public function shop(){
//        if(empty($this -> openId)){
//            return json(['code'=>2,'msg'=>ReturnCode::$return_code[2]]);
//        }
//        $res = UserShop::where('openid','=',$this->openId)->field('shopping')->find();
//        if($res['shopping']){
////            dump($res['shopping']);
//            $arr = object_array(json_decode($res['shopping']));
////            dump($arr);
//            foreach ($arr as $key => $val){
////                dump($val);
//                $name = db('goods')->where('goods_id','=',$val['goods_id'])->field('g_name')->find();
//                $val['goodsName'] = $name['g_name'];
//                foreach ($val['spec'] as $k => $v){
////                    dump($v);
//                    foreach ($v as $ke => $va){
//                        $s = db('set_'.$ke)->where($ke.'_id','=',$va)->find();
////                            $va->$e = $s;
//                        $v[$ke] = $s[$ke.'_name'];
//                    }
////                    dump($v);
//                    $val['spec'] = implode(',',$v);
//                }
//                $arr[$key] = $val;
//            }
//            $res['shopping'] = $arr;
//        }
//        if($res){
//            return json(['code'=>0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
//        }
//        return json(['code'=>1,'msg'=>ReturnCode::$return_code[1]]);
//    }
//
//    //购物袋列表  分开
//    public function shoplist(){
//        if(empty($this -> openId)){
//            return json(['code'=>2,'msg'=>ReturnCode::$return_code[2]]);
//        }
//        $res = UserShop::where('openid','=',$this -> openId)->select();
//
//        if($res){
//            return json(['code'=>0,'msg'=>ReturnCode::$return_code[0],'data'=>$res]);
//        }
//        return json(['code'=>1,'msg'=>ReturnCode::$return_code[1]]);
//    }
//
//    //加入购物袋
//    public function shopjoin(){
//
//        if(empty($this -> openId)){
//            return json(['code'=>2,'msg'=>ReturnCode::$return_code[2]]);
//        }
//        $data = ['openid' => $this -> openId,];
//        $res = UserShop::create($data);
//        if($res){
//            return json(['code'=>0,'msg'=>ReturnCode::$return_code[0]]);
//        }
//        return json(['code'=>1,'msg'=>ReturnCode::$return_code[1]]);
//    }
//
//    //减
//    public function shopreduce(){
//        if(empty($this -> openId)){
//            return json(['code'=>2,'msg'=>ReturnCode::$return_code[2]]);
//        }
//        $data = ['openid' => $this -> openId,];
//        $res = UserShop::create($data);
//        if($res){
//            return json(['code'=>0,'msg'=>ReturnCode::$return_code[0]]);
//        }
//        return json(['code'=>1,'msg'=>ReturnCode::$return_code[1]]);
//    }
}